<template>
  <div class="user-container">
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list class="user-list" v-model:loading="loading" :finished="page >= lastPage" finished-text="没有更多了"
        @load="onLoadMore">
        <UserItem @change-status="onChangeStatus"
          @clickControl="$router.push({ path: '/luckysjm', query: { id: user.id, status: user.value } })" @copy="copy"
          v-for="user in luckDrawsLists" :key="user.id" :user="user" />


      </van-list>
      <el-dialog v-model="dialogVisible" title="提示" width="300" :before-close="handleClose">
        <span>复制{{ copyNum }}？</span>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="enetrCopy">
              确认
            </el-button>
          </div>
        </template>
      </el-dialog>
      <van-back-top bottom="10vh" />
    </van-pull-refresh>
  </div>
</template>
<script setup>
import { onMounted, ref, watch } from 'vue'
import { luckDrawsList, luckDrawsStatus, luckDrawsCopy } from '@/api/lucky/index'
import { showDialog, showNotify, showToast } from 'vant'
import UserItem from '@/components/luckyItem.vue'

const refreshing = ref(false)
const page = ref(1)
const lastPage = ref(1)
const loading = ref(false)
const dialogVisible = ref(false)
const luckDrawsLists = ref([])
const copyId = ref('')
const copyNum = ref('')

onMounted(() => {
  refreshing.value = true
  getluckDrawsList()
})


const onLoadMore = () => {
  if (page.value >= lastPage.value) return
  getluckDrawsList()
  onRefresh()
}
const onRefresh = () => {
  page.value = 1
  lastPage.value = 1
  refreshing.value = true
  getluckDrawsList()
}
const getluckDrawsList = async () => {
  try {
    const result = await luckDrawsList({
      page: page.value,
      limit: 10000,
    })
    if (result.code === 200) {
      luckDrawsLists.value = []
      if (page.value === 1) {
        luckDrawsLists.value = result.data.data
      } else {
        luckDrawsLists.value = luckDrawsLists.value.concat(result.data.data)
      }
      page.value++
      lastPage.value = result.data.last_page
    }

  } catch (e) {
    console.log(e)
  } finally {
    loading.value = false
    refreshing.value = false
  }
}

const copy = async (e) => {
  copyId.value = e.id
  copyNum.value = e.name
  dialogVisible.value = true
  console.log(copyId.value)


}
const enetrCopy = async () => {
  try {
    const result = await luckDrawsCopy(
      copyId.value
    )
    if (result.code === 200) {
      showNotify({
        type: 'success',
        message: '复制成功'
      })

      dialogVisible.value = false
      onRefresh()
    }

  } catch (e) {
    console.log(e)
  } finally {
    loading.value = false
    refreshing.value = false
  }
}
const onChangeStatus = async (e) => {
  try {
    const result = await luckDrawsStatus(e.id, { status: e.status ? 1 : 0 })
    if (result.code === 200) {
      showNotify({
        type: 'success',
        message: '修改成功'
      })
      getluckDrawsList()
    } else {
      throw new Error()
    }
  } catch (e) {
    showNotify({
      type: 'danger',
      message: '操作失败'
    })
  }
}

</script>
<style scoped lang="less"></style>